package com.zl.cms.controller;

import com.zl.cms.auth.AuthClass;
import com.zl.cms.model.ChannelTree;
import com.zl.cms.model.Group;
import com.zl.cms.service.IGroupService;
import com.zl.cms.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-1-26 Time: 下午1:51
 */
@Controller
@RequestMapping("/admin/group")
@AuthClass
public class GroupController {
  private IGroupService groupService;
  private IUserService userService;

  public IUserService getUserService() {
    return userService;
  }

  @Inject
  public void setUserService(IUserService userService) {
    this.userService = userService;
  }

  public IGroupService getGroupService() {
    return groupService;
  }

  @Inject
  public void setGroupService(IGroupService groupService) {
    this.groupService = groupService;
  }

  @RequestMapping(value = "/groups", method = RequestMethod.GET)
  public String list(Model model) {
    model.addAttribute("datas", groupService.findGroup());
    return "group/list";
  }

  @RequestMapping(value = "/add", method = RequestMethod.GET)
  public String add(Model model) {
    model.addAttribute(new Group());
    return "group/add";
  }

  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public String add(@Valid Group group, BindingResult br) {
    if (br.hasErrors()) {
      return "group/add";
    }
    groupService.add(group);
    return "redirect:/admin/group/groups";
  }

  @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
  public String update(@PathVariable int id, Model model) {
    model.addAttribute(groupService.load(id));
    return "group/update";
  }

  @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
  public String update(@PathVariable int id, @Valid Group group, BindingResult br) {
    if (br.hasErrors()) {
      return "group/update";
    }
    Group og = groupService.load(id);
    og.setDescr(group.getDescr());
    og.setName(group.getName());
    groupService.update(og);
    return "redirect:/admin/group/groups";
  }

  @RequestMapping(value = "/delete/{id}")
  public String delete(@PathVariable int id) {
    groupService.delete(id);
    return "redirect:/admin/group/groups";
  }

  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public String show(@PathVariable int id, Model model) {
    model.addAttribute(groupService.load(id));
    model.addAttribute("users", userService.listGroupUsers(id));
    return "group/show";
  }

  @RequestMapping("/cleanusers/{id}")
  public String cleanGroupUsers(@PathVariable int id) {
    groupService.deleteGroupsUsers(id);
    return "redirect:/admin/group/groups";
  }

  @RequestMapping("/listChannels/{gid}")
  public String listChannels(@PathVariable int gid, Model model) {
    model.addAttribute("group", groupService.load(gid));
    return "group/listChannel";
  }

  @RequestMapping("/groupTree/{gid}")
  public @ResponseBody
  List<ChannelTree> groupTree(@PathVariable Integer gid) {
    return new ArrayList<>(groupService.generateGroupChannelTree(gid));
  }

  @RequestMapping("/setchannels/{gid}")
  public String setChannels(@PathVariable int gid, Model model) {
    model.addAttribute("group", groupService.load(gid));
    model.addAttribute("cids", groupService.listGroupChannelIds(gid));
    return "group/setChannels";
  }

  @RequestMapping(value = "/addGroupChannel", method = RequestMethod.POST)
  public void addGroupChannel(@RequestParam("gid") String gid, @RequestParam("ids") String ids) {
    int groupid = Integer.parseInt(gid);
    String[] channelids = ids.split(",");
    for (String c : channelids) {
      groupService.addGroupChannel(groupid, Integer.parseInt(c));
    }
  }

  @RequestMapping(value = "/delGroupChannel",method = RequestMethod.POST)
  public void delGroupChannel(HttpServletRequest request) {
    int groupid = Integer.parseInt(request.getParameter("gid"));
    String[] channelids = request.getParameter("ids").split(",");
    for (String c : channelids) {
      groupService.deleteGroupChannel(groupid, Integer.parseInt(c));
    }
  }


}
